package com.bleeth.flow.web.controller;

import cn.hutool.json.JSONObject;
import com.bleeth.flow.core.util.PluginUtil;
import com.bleeth.flow.core.util.SparkUtil;
import com.bleeth.flow.web.dao.XyFlowProjectRepository;
import com.bleeth.flow.web.view.DataQueryRequest;
import com.bleeth.flow.web.view.DataSaveRequest;
import com.bleeth.flow.web.view.RequestBaseVO;
import com.bleeth.flow.web.view.ResponseBaseVO;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.erupt.core.constant.EruptRestPath;

@RestController
@RequestMapping(EruptRestPath.ERUPT_API+"/api/data")
public class DataController {

    private Logger logger = LoggerFactory.getLogger(DataController.class);

    @Autowired
    private XyFlowProjectRepository xyDataModelRepository; //使用方式与 mybatis-plus 大同小异

    @PostMapping("/save")
    public ResponseBaseVO saveData(@RequestBody RequestBaseVO<DataSaveRequest> requestVO) {
        Dataset<Row> ds = SparkUtil.getSparkInstance().read()
                .format("jdbc")
                .option("driver", "com.mysql.jdbc.Driver")
                .option("url", "jdbc:mysql://127.0.0.1:3306/data_gear?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false")
                .option("user", "root")
                .option("password", "123456")
                .option("dbtable", "student")
                .load();
        ds.show(10);
        return ResponseBaseVO.ok("保存成功");
    }

    @PostMapping("/query")
    public ResponseBaseVO queryData(@RequestBody RequestBaseVO<DataQueryRequest> requestVO) {
        DataQueryRequest request = requestVO.getParam();

        return ResponseBaseVO.ok();
    }


    @PostMapping("/scan")
    public JSONObject scan(){
        PluginUtil.scanPathPlugin();
        return new JSONObject().set("nice","ok");
    }

    public static void main(String[] args) {
        PluginUtil.scanPathPlugin();
    }
}